<%@page contentType="text/html; charset=UTF-8"  language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

/**
 * 
 */
var jsession='<c:out value="${pageContext.session.id };"></c:out>';
var gPanelMargin = '100,0,0,0';

Ext.onReady(function(){
	var formLogin = Ext.create('Ext.form.Panel',{
		id: 'login',
		title: 'Login',
		url: 'login.do?jsessionid='+jsession,
		layout: {
			type: 'vbox',
			align: 'center'
		},
		width: '400px',
		height:'250px',
		items: [{
			fieldLabel: 'Username',
			name: 'utenti.username',
			xtype:'textfield',
			margin: '5,5,5,5',
			allowBlank: false
		},{	fieldLabel: 'Password',
			name: 'utenti.password',
			xtype:'textfield',
			inputType: 'password',
			margin: '5,5,5,5',
			allowBlank: false
		}
		]
		,
		fbar: [{
			xtype: 'button',
			text: 'Ok',
			formBind: true,
			disabled: false,
			handler: function(){
				var form = this.up('form').getForm();
				if(form.isValid()){
					doLogin(this,form);
	    		}
			}
		},{
			xtype: 'button',
			text: 'Annulla',
			handler: function(){
	    		  this.up('form').getForm().reset();
			}
		}
		       
		],
		border: '0,0,0,0',
		margin: gPanelMargin,
		renderTo: Ext.get('principale')
	});
	
});
	

function doLogin(theButton,theForm){
	var _params = theForm.getValues();
	Ext.Ajax.request({
	    url: 'login.do?jsessionid='+jsession,
	    method: 'post',
	    params: _params,
	    success: function(response){
	    	alert(response.responseText);
	        var text = eval(response.responseText);
	        if(!text[0].result){
	        	var msg = '';
	        	for(var i=0 ; i < text[0].messages.length;i++){
	        		msg+=text[0].messages[i]+'\n\r';
	        	}
	        	Ext.Msg.alert('Failure', msg);
	    	}else{
	    		eval(text[0].method);
	    	}
		}
	});
	
}

function drawAdminWindow(){
	Ext.get('login').setVisible(false);
	Ext.create('Ext.data.Store', {
    storeId:'simpsonsStore',
    fields:["name", "email", "phone"],
    data:{'items':[
        {"name":"Lisa", "email":"lisa@simpsons.com", "phone":"555-111-1224"},
        {"name":"Bart", "email":"bart@simpsons.com", "phone":"555--222-1234"},
        {"name":"Homer", "email":"home@simpsons.com", "phone":"555-222-1244"},                        
        {"name":"Marge", "email":"marge@simpsons.com", "phone":"555-222-1254"}            
    ]},
    proxy: {
        type: 'memory',
        reader: {
            type: 'json',
            root: 'items'
        }
    }
});

Ext.create('Ext.grid.Panel', {
    title: 'Simpsons',
    store: Ext.data.StoreManager.lookup('simpsonsStore'),
    columns: [
        {dataIndex: "name",header: "Name"  },
        {dataIndex: "email",header: "Email",  flex:1},
        {dataIndex: "phone",header: "Phone" }
    ],
    height: '480px',
    width: '100%',
    renderTo: Ext.get('principale')	
});
}
